package com.app.micaihu.view.army.activity

import android.view.View
import android.widget.ArrayAdapter
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.utils.ImageLoadUtils
import com.app.micaihu.view.army.contract.ArmyCreateContract
import com.app.micaihu.view.army.dialog.ArmyCreateHeadDialog
import com.app.micaihu.view.army.dialog.ArmyCreateSuccessDialog
import com.app.micaihu.view.army.presenter.ArmyCreatePresenter
import com.app.micaihu.view.bean.ArmyCreateInfo
import com.app.micaihu.view.bean.ArmyIcon
import com.app.micaihu.view.main.topic.PostSendActivity
import com.app.micaihu.view.user.userinfo.activity.UserInforActivity
import com.app.micaihu.view.user.usertask.UserTaskActivity
import com.app.micaihu.view.utils.ViewUtils
import com.app.micaihu.view.utils.loadImage
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_create_activity.*

/**
 * 军团创建
 */
class ArmyCreateActivity : BaseMvpActivity<ArmyCreateContract.View, ArmyCreatePresenter>(), ArmyCreateContract.View {

    override fun getLayoutId() = R.layout.army_create_activity

    override fun getPresenter() = ArmyCreatePresenter()
    private var warZoneAdapter: ArrayAdapter<String>? = null
    private var armyIcon: String = ""
    private val armyCreateHeadDialog: ArmyCreateHeadDialog by lazy {
        ArmyCreateHeadDialog()
    }

    override fun onRestart() {
        super.onRestart()
        mPresenter?.createArmyIndex()
    }

    override fun setData() {
        mPresenter?.createArmyIndex()
    }

    override fun addListeners() {
        ivHeadPic.setOnClickListener {
            armyCreateHeadDialog.show(supportFragmentManager)
        }
        btnArmyCreate.setOnClickListener {
            createArmy()
        }
        armyCreateHeadDialog.setOnHeadClickListener(object : ArmyCreateHeadDialog.OnHeadClickListener {
            override fun onHeadClick(icon: String) {
                armyIcon = icon
                ImageLoadUtils.loadImage(icon, ivHeadPic)
            }
        })
        tvMobileStatus.setOnClickListener {
            enterPager(UserInforActivity::class.java)
        }
        tvSignStatus.setOnClickListener {
            enterPager(UserTaskActivity::class.java)
        }
        tvPostStatus.setOnClickListener {
            enterPager(PostSendActivity::class.java)
        }
    }

    private fun createArmy() {
        if (tvMobileStatus.isEnabled) {
            ToastUtils.showShort("请完成绑定手机任务")
            return
        }
        if (tvSignStatus.isEnabled) {
            ToastUtils.showShort("请完成签到任务")
            return
        }
        if (tvPostStatus.isEnabled) {
            ToastUtils.showShort("请完成发帖任务")
            return
        }
        if (armyIcon.isEmpty()) {
            ToastUtils.showShort("请选择军团头像")
            return
        }
        if (etArmyName.text.toString().isEmpty()) {
            ToastUtils.showShort("请输入军团名称")
            return
        }
        val selectTag = tagLayout.selectedTags
        if (selectTag.isEmpty()) {
            ToastUtils.showShort("请选择兴趣标签")
            return
        }
        var tag = ""
        for ((index, item) in selectTag.withIndex()) {
            tag = if (index == 0) {
                tag.plus(item)
            } else {
                tag.plus(",").plus(item)
            }
        }
        if (etArmyDesc.text.toString().isEmpty()) {
            ToastUtils.showShort("请输入军团简介")
            return
        }
        mPresenter?.createArmy(armyIcon, etArmyName.text.toString(), tag, spinner.selectedItem.toString(), etArmyDesc.text.toString())
    }

    override fun handleCreateArmyIndex(result: BaseBean<ArmyCreateInfo?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                result.data?.let { data ->
                    if (data.mobileStatus && data.signStatus && data.postStatus) {
                        llNoPower.visibility = View.GONE
                    } else {
                        llNoPower.visibility = View.VISIBLE
                    }
                    if (data.mobileStatus) {
                        tvMobileStatus.text = "已完成"
                        tvMobileStatus.setTextColor(ColorUtils.getColor(R.color.colorA5A5A5))
                    } else {
                        tvMobileStatus.text = "去完成"
                        tvMobileStatus.setTextColor(ColorUtils.getColor(R.color.app_theme))
                    }
                    tvMobileStatus.isEnabled = !data.mobileStatus

                    if (data.signStatus) {
                        tvSignStatus.text = "已完成"
                        tvSignStatus.setTextColor(ColorUtils.getColor(R.color.colorA5A5A5))
                    } else {
                        tvSignStatus.text = "去完成"
                        tvSignStatus.setTextColor(ColorUtils.getColor(R.color.app_theme))
                    }
                    tvSignStatus.isEnabled = !data.signStatus

                    if (data.postStatus) {
                        tvPostStatus.text = "已完成"
                        tvPostStatus.setTextColor(ColorUtils.getColor(R.color.colorA5A5A5))
                    } else {
                        tvPostStatus.text = "去完成"
                        tvPostStatus.setTextColor(ColorUtils.getColor(R.color.app_theme))
                    }
                    tvPostStatus.isEnabled = !data.postStatus

                    data.tag?.let {
                        tagLayout.setTags(it)
                    }
                    data.warZone?.let {
                        warZoneAdapter = ArrayAdapter(thisContext, R.layout.army_create_war_zone_item, it)
                        //设置下拉列表的风格
                        warZoneAdapter?.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spinner.adapter = warZoneAdapter
                    }
                    data.armyIcon?.let {
                        val list = arrayListOf<ArmyIcon>()
                        for ((index, item) in it.withIndex()) {
                            var armyIcon: ArmyIcon? = null
                            armyIcon = if (index == 0) {
                                ArmyIcon(item, true)
                            } else {
                                ArmyIcon(item, false)
                            }
                            list.add(armyIcon)
                            armyCreateHeadDialog.setIconData(list)
                        }
                    }
                    tvArmyNotice.text = data.notice
                    ViewUtils.changeViewScale(ivArmyNoticeImg, 0, 973, 383, SizeUtils.dp2px(20f))
                    ivArmyNoticeImg.loadImage(data.noticeImg)
                }
            }
        }
    }


    override fun handleCreateArmy(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.armyCreateSuccess).post(true)
                ArmyCreateSuccessDialog().show(supportFragmentManager)
            }
        }
    }
}